package service.impl;

import dao.IShopCarDao;
import model.ShopCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.IShopCarService;

import java.util.List;

/**
 * @aythor:侯仪澳
 * @data:2022/8/4
 */
@Service
public class ShopCarService implements IShopCarService {
    @Autowired
    private IShopCarDao shopCarDao;

    //添加到购物车   若购物车里存在该商品，数量加到原来商品的数量之上  若没有，添加新的商品购物车
    @Override
    public String addOneGoodsToCar(ShopCar shopCar) {
        ShopCar shopCar1 = shopCarDao.queryShopCarExist(shopCar);
        if (shopCar1 == null){
            int count = shopCarDao.addOneGoodsToCar(shopCar);
            if (count == 1){
                return "1";
            }else {
                return "0";
            }
        }else {
            ShopCar shopCar2 = new ShopCar();
            shopCar2.setBuyCount(shopCar.getBuyCount());
            shopCar2.setId(shopCar1.getId());
            int count = shopCarDao.updateShopCar(shopCar2);
            if (count == 1){
                return "1";
            }else {
                return "0";
            }
        }
    }

    //查询购物车
    @Override
    public List<ShopCar> queryShopCar(ShopCar shopCar) {
        return shopCarDao.queryShopCar(shopCar);
    }

    //删除购物车里的商品
    @Override
    public String deleteShopCar(Integer id) {
        int count = shopCarDao.deleteShopCar(id);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }
}
